package com.cgly.medical.service.impl;

import com.cgly.medical.config.XDelayedMessageConfig;
import com.cgly.medical.service.DelayedSendMessageService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangxiangli
 * @create 2022-08-28-17:40
 */
@Service
public class DelayedSendMessageServiceImpl implements DelayedSendMessageService {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void send(String endOrderId, Integer delayTime) {
        //将消息携带路由键值，异步发送

        rabbitTemplate.convertAndSend(
                XDelayedMessageConfig.DELAYED_EXCHANGE,
                XDelayedMessageConfig.ROUTING_KEY,
                endOrderId,
                message->{
                    //这里设置延迟的时间，然后把msg的内容进行返回，给到监听延迟队列的方法去
                    //其实也就是消费者
                    message.getMessageProperties().setDelay(delayTime);
                    return message;
                });
    }
}
